package top.soulblack.purchasesale.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.common.enums.OrderStatusEnum;
import top.soulblack.purchasesale.common.enums.ResultEnum;
import top.soulblack.purchasesale.exception.PurchaseException;
import top.soulblack.purchasesale.mapper.OrderMasterMapper;
import top.soulblack.purchasesale.model.DTO.OrderDTO;
import top.soulblack.purchasesale.model.OrderMaster;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: 韦溜溜
 * @Description:
 * @Date: Create in 20:13 2019/7/18
 */
@Service
@Slf4j
public class OrderMasterService {

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    public Boolean insertListMastert(List<OrderMaster> orderMasterList){
        int num = orderMasterMapper.insertOrderMasterList(orderMasterList);
        if(num>=1){
            return true;
        }else {
            log.error("【订单主体】插入失败 orderId={}",orderMasterList.get(0).getOrderId());
            throw new PurchaseException(ResultEnum.ORDERMASTERERROR);
        }
    }

    public Boolean inserMaster(OrderMaster orderMaster){
        int num = orderMasterMapper.insertOrderMaster(orderMaster);
        if(num>=1){
            return true;
        }else {
            log.error("【订单主体】插入失败 orderId={}",orderMaster.getOrderId());
            throw new PurchaseException(ResultEnum.ORDERMASTERERROR);
        }
    }

    public List<OrderDTO> selectListByAllShopId(Integer shopId){
        List<OrderDTO> orderDTOList = orderMasterMapper.selectListByAllShopId(shopId);
        if(orderDTOList.size()==0){
            log.error("【订单主体】查询失败，没有对应的订单 shopId={}",shopId);
            throw new PurchaseException(ResultEnum.ORDERSELECTERROR);
        }
        return orderDTOList;
    }

    public OrderDTO selectListByOrderId(String orderId){
        OrderDTO orderDTO = orderMasterMapper.selectListByOrderId(orderId);
        if(orderDTO==null){
            log.error("【订单主体】查询失败，没有对应的订单 orderId={}",orderId);
            throw new PurchaseException(ResultEnum.ORDERSELECTERROR);
        }
        return orderDTO;
    }

    public Boolean updateStatusByOrderId(String orderId,Integer status){
        int num = orderMasterMapper.updateStatuByOrderId(orderId,status);
        if(num>=1){
            return true;
        }
        else{
            log.error("【订单主体】更新失败 orderId={}",orderId);
            throw new PurchaseException(ResultEnum.UPDATEERROR);
        }
    }

    public Boolean updateRemarksByOrderId(String orderId,String remarks){
        int num = orderMasterMapper.updateRemarksByOrderId(orderId,remarks);
        if(num>=1){
            return true;
        }
        else{
            log.error("【订单主体】更新失败 orderId={}",orderId);
            throw new PurchaseException(ResultEnum.UPDATEERROR);
        }
    }

}
